<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>post json</title>
  </head>
  <body>
    <form action="/save" method="POST">
      <div>
        <label for="name"
          >name:
          <input type="text" name="name" id="name" value="Tom" />
        </label>
      </div>
      <div>
        <label for="age"
          >age:
          <input type="number" name="age" id="age" value="19" />
        </label>
      </div>
      <div>
        <label
          >hobbies:
          <br />
          <input
            type="text"
            name="hobbies[0]"
            id="hobbies[0]"
            value="reading"
          />
          <br />
          <input type="text" name="hobbies[1]" id="hobbies[1]" value="music" />
          <br />
          <input type="text" name="hobbies[2]" id="hobbies[2]" value="swim" />
        </label>
      </div>
      <button type="submit">Submit</button>
    </form>
    <script>
    function submitForm(event) {
        event.preventDefault();
        var formData = new FormData(myForm);
        let data = {};
        for (var [key, value] of formData.entries()) {
            if (key.startsWith("hobbies")) {
            data["hobbies"]
                ? data["hobbies"].push(value)
                : (data["hobbies"] = [value]);
            } else {
            data[key] = value;
            }
        }

        fetch("/save", {
            method: "POST",
            headers: {
            "Content-Type": "application/json"
            },
            body: JSON.stringify(data)
        })
            .then(function(response) {
            return response.json();
            })
            .then(function(response) {
            console.log(response);
            });
    }
</script>
<script>
    function submitForm(event) {
        event.preventDefault();
        var formData = new FormData(myForm);
        let data = {};
        for (var [key, value] of formData.entries()) {
            if (key.startsWith("hobbies")) {
            data["hobbies"]
                ? data["hobbies"].push(value)
                : (data["hobbies"] = [value]);
            } else {
            data[key] = value;
            }
        }

        fetch("http://127.0.0.1:5984/api/v1/user", {
            method: "POST",
            headers: {
            "Content-Type": "application/json"
            },
            body: JSON.stringify(data)
        })
            .then(function(response) {
            return response.json();
            })
            .then(function(response) {
            console.log(response);
            });
    }
</script>
  </body>
</html>